﻿if (layui.memberApi == undefined) {
    layui.extend({
        memberApi : '{/}' + Common.tplPath + '/layuiExts/member/memberApi'
    });
}
//全局函数
layui.use(['jquery', 'layer', 'util','memberApi','form'], function () {
    var $ = layui.jquery,
        layer = layui.layer,
        util = layui.util,
        form = layui.form,
        memberApi = layui.memberApi;
    util.fixbar();
    //导航控制
    master.start($);

    var active = {
        loginBtnClick:function() {
            var that = this;
            var load = Common.load();
            Common.open1("/ajax/login.do","用户登录");
            Common.close(load);
        },
        logoutBtnClick:function() {
            memberApi.logout(function() {
                document.location.href= Common.ctxPath + "/";
            });
        }
    };

    $('.layui-btn').on('click', function(){
        var othis = $(this), method = othis.data('method');
        active[method] ? active[method].call(this, othis) : '';
    });

    // 点赞
    $(document).on("click",".zan",function() {
        var articleId = $(this).attr("articleId");
        var self = this;
         Common.post("/core/zan/zan.json", {aId:articleId}, function(count) {
            $(self).find("span").html(count);
         })
    })

     form.verify({
        password: [/^[\S]{6,12}$/,'密码必须6到12位，且不能出现空格'],
        required1:function(value, item) {
            var verifyName = $(item).attr('name')
            , verifyType = $(item).attr('type')
            , verifyText = $(item).attr('lay-reqText')
            ,formElem = $(item).parents('.layui-form')//获取当前所在的form元素，如果存在的话
            ,verifyElem = formElem.find('input[name='+verifyName+']')//获取需要校验的元素
            ,isTrue = verifyElem.is(':checked')//是否命中校验
            ,focusElem = verifyElem.next().find('i.layui-icon');//焦点元素
            if(!isTrue || !value){
                //定位焦点
                focusElem.css(verifyType=='radio'?{"color":"#FF5722"}:{"border-color":"#FF5722"});
                //对非输入框设置焦点
                focusElem.first().attr("tabIndex","1").css("outline","0").blur(function() {
                    focusElem.css(verifyType=='radio'?{"color":""}:{"border-color":""});
                 }).focus();
                if (verifyText) {
                    return verifyText;
                }
                return '必填项不能为空';
            }
        },
        confirmPass:function(value){// 校验两次密码是否一致
            if($('input[name=password]').val() !== value)
                return '两次密码输入不一致！';
        },
        loginNameIsExist:function(value){
            var result = memberApi.loginNameVerify(value);
            if (result.code != 0) {
                return result.msg;
            }
        },
        domainIsExist:function(value){
            var result = memberApi.domainVerify(value);
            if (result.code != 0) {
                return result.msg;
            }
        },
        member_type:function(value) {
            //用户类型的验证
        },
        member_type1:function(value) {
            //用户类型1的验证
        },
        validVeirfyCode:function(value) {
            var result = null;
            $.ajax({
                url: "/core/validVeirfyCode",
                contentType: "application/json",
                dataType:'json',
                type: "get",
                async: false,
                data: {code:value,token:'reg'},
                success: function(data) {
                    if(data.code != 0) {
                        result = data.msg;
                    }
                }
            });
            return result;
        }
     });

});
var slider = 0;
var pathname = window.location.pathname.replace('Read', 'Article');
var master = {};
master.start = function ($) {
    $('#nav li').hover(function () {
        $(this).addClass('current');
    }, function () {
        var href = $(this).find('a').attr("href");
        if (pathname.indexOf(href) == -1) {
            $(this).removeClass('current');
        }
    });
    selectNav();
    function selectNav() {
        var navobjs = $("#nav a");
        $.each(navobjs, function () {
            var href = $(this).attr("href");
            if (pathname == href) {
                $(this).parent().addClass('current');
            }
        });
    };
    $('.phone-menu').on('click', function () {
        $('#nav').toggle(500);
    });
    $(".blog-user img").hover(function () {
        var tips = layer.tips('点击退出', '.blog-user', {
            tips: [3, '#009688'],
        });
    }, function () {
        layer.closeAll('tips');
    });
    var tempBtnTipsIndex;
    $(document).on('mouseenter',".btn-tips",function() {
        var title = $(this).attr("data-title");
        tempBtnTipsIndex = layer.tips(title, this, {
            tips: [1, '#3595CC']
        });
    });
    $(document).on('mouseleave',".btn-tips",function() {
        layer.close(tempBtnTipsIndex);
    });
};
